package org.example.day;

import java.util.HashSet;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/20 20:26
 */
public class Solution128 {

    public static void main(String[] args) {
        Solution128 solution128 = new Solution128();
        System.out.println(solution128.longestConsecutive(new int[]{100,4,200,1,3,2}));
    }

    public int longestConsecutive(int[] nums) {
        int longestStreak = 0;
        HashSet<Integer> numsSet = new HashSet<>();
        for (int num : nums) {
            numsSet.add(num);
        }

        for (int num : nums) {

            //判断是不是开头的序列
            if(!numsSet.contains(num-1)){
                int longStreak = 0;
                int curNum = num;
                while (numsSet.contains(curNum++)){
                    longStreak++;
                }
                longestStreak = Math.max(longestStreak, longStreak);
            }
        }

        return longestStreak;
    }

}
